package com.sunlg.smarthome.ui.warning;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sunlg.smarthome.R;
import com.sunlg.smarthome.base.BaseFragment;
import com.sunlg.smarthome.contract.FireWarningContract;
import com.sunlg.smarthome.contract.presenter.FireWarningPresenter;
import com.sunlg.smarthome.data.entity.FireWarning;
import com.sunlg.smarthome.data.entity.Warning;
import com.sunlg.smarthome.ui.wedget.RecyclerViewDivider;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class FireWarningFragment extends BaseFragment
        implements FireWarningContract.View{
    private RecyclerView rvWarningList;
    private WarningAdapter warningAdapter;
    private List<Warning> warningList;
    private FireWarningContract.Presenter presenter;

    public FireWarningFragment() {
        // Required empty public constructor
    }

    @Override
    protected int getLayoutResourceId() {
        return R.layout.fragment_fire_warning;
    }

    @Override
    protected void init() {
        warningList = new ArrayList<>();
        warningAdapter = new WarningAdapter(getActivity(), warningList);
        presenter = new FireWarningPresenter();
        presenter.attachView(this);
    }

    @Override
    protected void initView(View view) {
        rvWarningList = (RecyclerView)view.findViewById(R.id.rv_warning_list);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        rvWarningList.setLayoutManager(layoutManager);
        rvWarningList.setAdapter(warningAdapter);
        rvWarningList.addItemDecoration(new RecyclerViewDivider(getActivity(), LinearLayoutManager.VERTICAL));
    }

    @Override
    public void onStart() {
        super.onStart();
        presenter.loadAllFireWarning();
    }

    @Override
    public void loadFireWarningSuccess(List<FireWarning> warnings) {
        warningList.clear();
        warningList.addAll(warnings);
        warningAdapter.notifyDataSetChanged();
    }

    @Override
    public void showError(String msg) {
        toast(msg);
    }
}
